﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccessLayer;
using BusinessLogicLayer;

namespace Maintain_supplier_information
{
    public partial class ChangeCollectionPointDetails : System.Web.UI.Page
    {
        String userName;
        ChangeCollectionControl changeCollection;
        SupplierBizlogic sbz;

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                changeCollection = new ChangeCollectionControl();
                sbz = new SupplierBizlogic();

                #region Check if user exists and has permission to view this page
                if (Session["UserLoggedIn"] != null)
                {
                    List<string> list = Session["UserLoggedIn"] as List<string>;
                    if (list[0].Equals("validuser"))
                    {
                        int roleId = Convert.ToInt32(list[6]);
                        if (roleId != 5)
                        {
                            Response.Redirect("/AccessDenied.aspx");

                        }
                        userName = list[3];
                    }
                    else
                    {
                        Response.Redirect("/Login.aspx");
                    }
                }
                else
                {
                    Response.Redirect("/Login.aspx");
                }
                #endregion

                if (!IsPostBack)
                {
                    ddl_CollectionPoint.DataSource = changeCollection.getCollectionPointsLst();
                    ddl_CollectionPoint.DataTextField = "CollectionPointName";
                    ddl_CollectionPoint.DataValueField = "CollectionPointID";
                    ddl_CollectionPoint.DataBind();

                    DateTime dt = changeCollection.getFixCollectionTime(Convert.ToInt32(ddl_CollectionPoint.SelectedValue));
                    String colTime = dt.ToShortTimeString();
                    txt_CollectionPointTime.Text = colTime;

                    CollectionPointDetail cp = changeCollection.getOneCollectionPointDetail(Convert.ToInt32(ddl_CollectionPoint.SelectedValue));
                    string colDate = cp.CollectionDate.ToShortDateString();
                    txt_CollectionDate.Text = colDate;
                }

                lbl_status.Text = "";
                lblMessage.Text = "";

                // Block the buttons to prevent multiple clicks by the user
                btn_Change.Attributes.Add("onclick", "this.disabled=true;" + GetPostBackEventReference(btn_Change).ToString() + ";");
            }
            catch (Exception ex)
            {
                // Release block button
                btn_Change.Enabled = true;
            }
        }

        //protected void btn_search_Click(object sender, EventArgs e)
        //{
        //    //se = new StationeryInventoryEntities1();

        //    int id = Convert.ToInt32(ddl_CollectionPoint.SelectedValue.ToString());

        //    CollectionPointDetail cp = changeCollection.getOneCollectionPoint(id);

        //    txt_CollectionDate.Text = cp.CollectionDate.ToShortDateString();

        //    //time will always give fix time
        //    DateTime dt = changeCollection.getFixCollectionTime(Convert.ToInt32(ddl_CollectionPoint.SelectedValue));
        //    String colTime = dt.ToShortTimeString();
        //    txt_CollectionPointTime.Text = colTime;


        //    //txt_CollectionPointTime.Text = cp.CollectionTime.ToShortTimeString();


        //}

        protected void Calendar1_SelectionChanged(object sender, EventArgs e)
        {
            try
            {
                String sDate = Calendar1.SelectedDate.ToShortDateString();
                DateTime startDate = Convert.ToDateTime(sDate);
                if (startDate < DateTime.Today)
                {
                    txt_CollectionDate.Text = "Invalid";
                }
                else
                {
                    txt_CollectionDate.Text = sDate;
                    Calendar1.SelectedDates.Clear();
                    Calendar1.Visible = false;
                }
            }
            catch (Exception ex)
            {
            }
        }

        protected void btn_Change_Click(object sender, EventArgs e)
        {
            try
            {
                Calendar1.Visible = false;
                //SupplierBizlogic sbz = new SupplierBizlogic();   
                //SupplierDataAccess sad = new SupplierDataAccess(); 
                //se = new StationeryInventoryEntities1();

                int id = Convert.ToInt32(ddl_CollectionPoint.SelectedValue.ToString());
                DateTime collectionDate = Convert.ToDateTime(txt_CollectionDate.Text);
                DateTime collectionTime = Convert.ToDateTime(txt_CollectionPointTime.Text);

                //after save the collection point detail, send email to notify 
                changeCollection.saveNewCollectionPointDetail(id, collectionDate, collectionTime, userName);

                lbl_status.Text = "Notification has sent to all respective employee for changes";

                #region Commented Out
                //for (int i = 0; i < emailList.Count; i++)
                //{
                //    sbz.sendnotification(empId, username, status, collPoint, collDate, collTime);
                //}   
                //if (Convert.ToDateTime(txt_CollectionDate.Text) > DateTime.Today)
                //{
                //    CollectionPointDetail cp = new CollectionPointDetail();
                //    cp = se.CollectionPointDetails.FirstOrDefault(x => x.CollectionPointID == id && x.CollectionDate > DateTime.Today);
                //    if (cp != null)
                //    {
                //        cp.CollectionDate = Convert.ToDateTime(txt_CollectionDate.Text);
                //        cp.CollectionTime = Convert.ToDateTime(txt_CollectionPointTime.Text);
                //        se.SaveChanges();
                //        lbl_status.Text = "Updated"; 
                //        List<Employee> empList = sad.getemployeelist();
                //        foreach(Employee emp in empList)
                //        {
                //            int empId = emp.EmployeeID;
                //            //Fake user
                //            string username = "user1";
                //            string status = lbl_status.Text;
                //            string collPoint = cp.CollectionPoint.CollectionPointName;
                //            DateTime collDate = cp.CollectionDate;
                //            DateTime collTime = cp.CollectionTime;
                //           sbz.sendnotification(empId,username,status,collPoint,collDate,collTime);
                //            break;
                //        }
                //    }
                //    else
                //    {     
                //        CollectionPointDetail cp1 = new CollectionPointDetail();
                //        cp1.CollectionPointID = id;
                //        cp1.CollectionDate = Convert.ToDateTime(txt_CollectionDate.Text);
                //        cp1.CollectionTime = Convert.ToDateTime(txt_CollectionPointTime.Text);
                //        se.CollectionPointDetails.Add(cp1);
                //        se.SaveChanges(); 
                //    }
                //}
                //else
                //{
                //    lbl_status.Text = "Date error";
                //}
                #endregion
            }
            catch (FormatException ex)
            {
                btn_Change.Enabled = true;
                lblMessage.Text = "Please enter a valid time (hh:mm AM|PM)";
            }
            catch (Exception ex)
            {
                btn_Change.Enabled = true;
            }  
        }

        protected void ddl_CollectionPoint_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                int id = Convert.ToInt32(ddl_CollectionPoint.SelectedValue.ToString());

                CollectionPointDetail cp = changeCollection.getOneCollectionPoint(id);

                txt_CollectionDate.Text = cp.CollectionDate.ToShortDateString();

                //time will always give fix time
                DateTime dt = changeCollection.getFixCollectionTime(Convert.ToInt32(ddl_CollectionPoint.SelectedValue));
                String colTime = dt.ToShortTimeString();
                txt_CollectionPointTime.Text = colTime;
            }
            catch (Exception ex)
            {
            }
        }

        protected void btnStartDate_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                Calendar1.Visible = true;
            }
            catch (Exception ex)
            {
            }
        }

        protected void ddl_CollectionPoint_SelectedIndexChanged1(object sender, EventArgs e)
        {
            
        }

        protected void btn_Cancel_Click(object sender, EventArgs e)
        {
            try
            {
                Calendar1.Visible = false;
                int index = Convert.ToInt32(ddl_CollectionPoint.Items[0].Value);
                ddl_CollectionPoint.SelectedIndex = 0;
                DateTime dt = changeCollection.getFixCollectionTime(index);
                String colTime = dt.ToShortTimeString();
                txt_CollectionPointTime.Text = colTime;

                CollectionPointDetail cp = changeCollection.getOneCollectionPointDetail(Convert.ToInt32(ddl_CollectionPoint.SelectedValue));
                string colDate = cp.CollectionDate.ToShortDateString();
                txt_CollectionDate.Text = colDate;
            }
            catch (Exception ex)
            {
            }
        }
    }
}